#include "ModulsManager\ClassDINmodul.h"

const DINsets DINdflt = {
                          0x00,//DIchTyp
                          0x00,//EdgeTyp
                          0x3F,//DIchRst
                          {0,0,0,0,0,0,0,0},//Kd[]
                          0x00,//ChPwrType
                          0x0F,//ChMeas
                          0x0F,//ChPwrMod1
                          0x00,//ChPwrMod2
                          0,//MeasDelay
                          0,//PwrOnDelay
                         };

/****************************************************************************************************/
/*                           D I N _ M O D U L E      C O N S T R U C T O R                         */
/****************************************************************************************************/
DINmodul::DINmodul(uint8_t devID, uint8_t modKey, DinData *pResult) : BASEmodul(devID,modKey)
{
  pData = pResult; pDINsets = &DINdflt;
}

DINmodul::DINmodul(uint8_t devID, uint8_t modKey, DinData *pResult, DINsets const *pSets) : BASEmodul(devID, modKey)
{
  pData = pResult;  pDINsets = pSets;
}



STATUS_MODUL DINmodul::ReadCHvals(void)
{
  uint32_t ch=0, nChMax;    STATUS_MODUL status;
//
  status = RdMod(aDSCRTst,(NofCh<<2)+4);
  nChMax = NofCh<NofChMax ?   NofCh : NofChMax;
//  
  if(status==ALL_RIGHT)
    { 
      pData->DIN_State = BufToUSHT(0);
      pData->DIN_Activ = BufToUSHT(2);
      for(ch=0; ch<nChMax; ++ch)
       {  pData->Count[ch] = BufToUINT((ch<<2)+4);  }
      while(ch<NofChMax)  {pData->Count[ch++] = 0;} 
    }
  return status;
}



STATUS_MODUL DINmodul::SetUpModul(void)
{
  STATUS_MODUL status;
//
  status = ReadModInfo(MinModTypeDIN);
  if(status==ALL_RIGHT)
    { pData->NofCH = NofCh;  status = WriteDINsets(); }
  if(status==ALL_RIGHT) 
    { /*WrCR1(WrFALSH);*/  WrKey(); }
//
  return status;
}


STATUS_MODUL DINmodul::WriteDINsets(void)
{
  STATUS_MODUL status;
//
/*  pDINsets->DIchTyp &= CHmask;    pDINsets->EdgeTyp &= CHmask;    pDINsets->DIchRst &= CHmask;    
  pDINsets->ChPwrType &= CHmask;  pDINsets->ChMeas &= CHmask;
  pDINsets->ChPwrMod1 &= CHmask;  pDINsets->ChPwrMod2 &= CHmask;*/
//
  status = WrMod(aDiChTyp,2,&pDINsets->DIchTyp);
  if(status==ALL_RIGHT) status = WrMod(aEdgeTyp,2,&pDINsets->EdgeTyp);
  if(status==ALL_RIGHT) status = WrMod(aRstEn,2,&pDINsets->DIchRst);
  if(status==ALL_RIGHT) status = WrMod(aKd,NofCh<<1,&pDINsets->Kd);
  if(status==ALL_RIGHT) status = WrMod(aChPwrTyp,2,&pDINsets->ChPwrType);
  if(status==ALL_RIGHT) status = WrMod(aChEn,2,&pDINsets->ChMeas);
  if(status==ALL_RIGHT) status = WrMod(aPwrMod1,2,&pDINsets->ChPwrMod1);
  if(status==ALL_RIGHT) status = WrMod(aPwrMod2,2,&pDINsets->ChPwrMod2);
  if(status==ALL_RIGHT) status = WrMod(aMeasBreak,2,&pDINsets->MeasDelay);
  if(status==ALL_RIGHT) status = WrMod(aPwrDelay,2,&pDINsets->PwrOnDelay);
//
  return status;
}

void DINmodul::SetUpModState(void)
{
  pData->Pwr_State = BufToUSHT(aChPwr-aCR1st);
  pData->ChSC_State = BufToUSHT(aChSC-aCR1st);
}


